import Vue from 'vue'
import VueRouter from 'vue-router'
import home from "@/views/Home/home.vue"
import details from "@/views/Details/details.vue"
import shoppingCart from "@/views/shoppingCart/shoppingCart.vue"
import goodsListMore from '@/views/GoodsListMore/goodsListMore.vue'
import counter from '@/views/Counter/counter.vue'
import test from '@/views/test/test.vue'
import my from '@/views/My/my.vue'
import comment from '@/views/Comment/comment.vue'

//进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    component: home,
  },
  {
    path: '/details',
    component: details,
  },
  {
    path: '/shoppingCart',
    component: shoppingCart,
  },
  {
    path: '/goodsListMore',
    component: goodsListMore,
  },
  {
    path:'/counter',
    component:counter
  },
  {
    path:'/comment',
    component:comment
  },
  {
    path:'/my',
    component:my,
    children:[
      {path:'/address',component:()=>import('@/views/My/my_item/address.vue')},
      {path:'/info',component:()=>import('@/views/My/my_item/info.vue')},
      {path:'/foot',component:()=>import('@/views/My/my_item/foot.vue')},
      {path:'/recharge',component:()=>import('@/views/My/my_item/recharge.vue')},
      {path:'/collect',component:()=>import('@/views/My/my_item/collect.vue')},
      {path:'/order',component:()=>import('@/views/My/my_item/order.vue')}
    ]
  },
  {
    path:'/test',
    component:test
  },
  {
    path: '/Error',
    component: () => import('@/components/modules/Error.vue'),
    hidden: true
  },

  //这个*匹配必须放在最后，将改路由配置放到所有路由的配置信息的最后，否则会其他路由path匹配造成影响。     
  {
    path: '*',
    redirect: '/Error',
    hidden: true
  }

]

// // 保存原来的push函数
// const routerPush = VueRouter.prototype.push
// // 重写push函数
// VueRouter.prototype.push = function push(location) {
//   // 这个if语句在跳转相同路径的时候，在路径末尾添加新参数（一些随机数字）
//   if (typeof (location) == "string") {
//     var Separator = "&";
//     if (location.indexOf('?') == -1) { Separator = '?'; }
//     location = location + Separator + "random=" + Math.random();
//   }
//   // 捕获异常
//   return routerPush.call(this, location).catch(error => error)
// }







const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // if (to.path == '/shoppingCart') {
  //   const user = JSON.parse(localStorage.getItem("user"));
  //   if (user) {
  //     next()
  //     NProgress.start() // 进度条开始
  //   } else {
  //     alert('未登录')
  //     next('/home')
  //   }
  // }
  NProgress.start() // 进度条开始
  next()
})

NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 1000, // 递增进度条的速度
  showSpinner: true, // 是否显示加载 icon
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

router.afterEach(() => {
  NProgress.done() // 进度条结束
})


export default router
